package thread;

public class InterruptDemo implements Runnable{
	private double d = 0;
	public void run() {
		try {
			//这里一般是用interrupted()来判断并清楚标志,
			//while(true){
			while(!Thread.interrupted()){
				System.out.println("正在执行...");
				//队列中属于就绪状态，所以去掉sleep将无法打断，被打断和判断终止区别从输出能看出来
				Thread.sleep(20);
				System.out.println("计算...");
				for(int i=0;i<10000;i++){
					d=d+(Math.PI+Math.E)/d;
				}
				System.out.println(d);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("异常打断");
			e.printStackTrace();
		}
		System.out.println("被终止了");
	}
	public static void main(String[] args) throws InterruptedException {
		//如果线程是阻塞的，调用
		Thread t = new Thread(new InterruptDemo());
		t.start();
		Thread.sleep(100);
		System.out.println("准备打断");
		t.interrupt();
		System.out.println("进行了打断");
	}
}
